// 统一封装接口方法，每一个方法负责请求一个url地址，逻辑页面导入这个方法就可以发起请求
import axios from '@/utils/request.js'
import { getStorage } from '@/utils/storage.js'

// 用户-登录接口
export const loginAPI = ({ mobile, code }) =>
  axios({
    url: '/v1_0/authorizations',
    method: 'POST',
    data: {
      mobile,
      code
    }
  })

// 用户-关注
export const userFollowedAPI = ({ target }) =>
  axios({
    url: '/v1_0/user/followings',
    method: 'POST',
    data: {
      target
    }
  })

// 用户-取关
export const userUnFollowedAPI = ({ target }) =>
  axios({
    url: `/v1_0/user/followings/${target}`,
    method: 'DELETE'
  })

// 用户-获取个人资料
export const userProfileAPI = () =>
  axios({
    url: '/v1_0/user/profile'
  })

// 用户-获取基本信息（我的界面数据）
export const getUserAPI = () =>
  axios({
    url: '/v1_0/user'
  })

// 用户-修改头像
export const updateUserPhotoAPI = (fd) =>
  axios({
    url: '/v1_0/user/photo',
    method: 'PATCH',
    data: fd // fd是外面传进来的表单对象
  })

// 用户-更新基本资料
export const updateUserProfileAPI = (dataObj) => {
  const obj = {
    name: '',
    gender: 0,
    birthday: '',
    intro: ''
  }
  for (const key in obj) {
    if (dataObj[key] === undefined) {
      delete obj[key]
    } else {
      obj[key] = dataObj[key]
    }
  }
  return axios({
    url: '/v1_0/user/profile',
    method: 'PATCH',
    data: obj
  })
}

// 用户-刷新token
export const getNewTokenAPI = () =>
  axios({
    url: '/v1_0/authorizations',
    method: 'PUT',
    headers: {
      Authorization: 'Bearer ' + getStorage('refresh_token')
    }
  })

// 频道-获取所有
export const getAllChannelsAPI = () =>
  axios({
    url: '/v1_0/channels',
    method: 'GET'
  })

// 频道-获取用户选择 注意：用户没有登录默认返回后台设置的默认频道
export const getUserChannelsAPI = () =>
  axios({
    url: '/v1_0/user/channels'
  })

// 频道-更新覆盖
export const updateChanneksAPI = ({ channels }) =>
  axios({
    url: '/v1_0/user/channels',
    method: 'PUT',
    data: {
      channels // 用户已选这个频道数据
    }
  })

// 频道-删除用户指定
export const removeChannelAPI = ({ channelId }) => axios({
  url: `/v1_0/user/channels/${channelId}`,
  method: 'DELETE'
})

// 文章-列表
export const getArticleListAPI = ({ channelId, timestamp }) =>
  axios({
    url: '/v1_0/articles',
    method: 'GET',
    params: {
      channel_id: channelId,
      timestamp
    }
  })

// 文章-反馈不喜欢的
export const dislikeArticleAPI = ({ artId }) =>
  axios({
    url: '/v1_0/article/dislikes',
    method: 'POST',
    data: {
      target: artId
    }
  })

// 文章-反馈垃圾内容
export const reportArticleAPI = ({ artId, type }) =>
  axios({
    url: '/v1_0/article/reports',
    method: 'POST',
    data: {
      target: artId,
      type,
      remark: '想写可以在逻辑页面判断点击类型是0，给个输入框，传参打这里'
    }
  })

// 文章-获取详情
export const detailAPI = ({ artId }) =>
  axios({
    url: `/v1_0/articles/${artId}`
  })

// 文章-点赞
export const likeArticleAPI = ({ target }) =>
  axios({
    url: '/v1_0/article/likings',
    method: 'POST',
    data: {
      target
    }
  })

// 文章-取消点赞
export const unLikeArticleAPI = ({ target }) =>
  axios({
    url: `/v1_0/article/likings/${target}`,
    method: 'DELETE'
  })

// 文章-获取评论
export const commentsListAPI = ({ source, offset = null, limit = 10 }) =>
  axios({
    url: '/v1_0/comments',
    params: {
      type: 'a',
      source,
      offset,
      limit
    }
  })

// 文章-评论 - 喜欢
export const commentLikingAPI = ({ target }) =>
  axios({
    url: '/v1_0/comment/likings',
    method: 'POST',
    data: {
      target
    }
  })

// 文章-评论-取消喜欢
export const commentDisLikingAPI = ({ target }) =>
  axios({
    url: `/v1_0/comment/likings/${target}`,
    method: 'DELETE'
  })

// 文章-评论-发布评论
export const commentSendAPI = ({ target, content, artId = null }) => {
  // 形参的art_id是可选参数，如果是对文章评论，则不需要传递art_id所以这时，art_id为null则证明此次调用是针对文章评论的
  // data请求体需要自己封装
  const obj = {
    target,
    content
  }
  if (artId !== null) obj.art_id = artId
  return axios({
    url: '/v1_0/comments',
    method: 'POST',
    data: obj
  })
}

// 搜索-联想菜单
export const suggestListAPI = ({ keyWords }) =>
  axios({
    url: '/v1_0/suggestion',
    params: {
      q: keyWords
    }
  })

// 搜索-搜索结果列表
export const searchResultAPI = ({ page = 1, perPpage = 10, q }) =>
  axios({
    url: '/v1_0/search',
    params: {
      page,
      per_page: perPpage,
      q
    }
  })
